<template>
  <div class="mod-sp__spmatchitemteam">
    <el-form :inline="true" :model="dataForm" @keyup.enter="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.matchId" placeholder="赛事ID" clearable @change="queryMatchItemList"></el-input>
      </el-form-item>
      <el-form-item label="赛项" prop="province">
        <el-select style="width: 150px" placeholder="请选择" v-model="dataForm.matchItemId">
          <el-option value="" label="全部">全部</el-option>
          <el-option v-for="item in baseMatchList" :key="item.id" :label="item.baseMatchItemName" :value="item.id"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.phone" placeholder="选手手机号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="generateCertificate()" type="primary">重新生成获奖证书编号</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="id" width="80" label="序号" sortable="custom" header-align="center" align="center"></el-table-column>
      <el-table-column prop="teamName" width="250" label="代表队名称" header-align="center" align="center">
        <template v-slot="scope">
          <el-button type="text" @click="teamHandle(scope.row.teamId)">{{ scope.row.teamName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="company" width="250" label="选手单位" header-align="center" align="center"></el-table-column>
      <el-table-column prop="realName" width="200" label="选手" header-align="center" align="center">
        <template v-slot="scope">
          <el-button type="text" @click="userHandle(scope.row.userId)">{{ scope.row.realName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="ticket" width="150" label="选手编号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="score" v-if="scores[0]" width="100" :label="$t(scores[0])" header-align="center" align="center"></el-table-column>
      <el-table-column prop="score1" v-if="scores[1]" width="100" :label="$t(scores[1])" header-align="center" align="center"></el-table-column>
      <el-table-column prop="score2" v-if="scores[2]" width="100" :label="$t(scores[2])" header-align="center" align="center"></el-table-column>
      <el-table-column prop="ranks" width="100" label="名次" sortable="custom" header-align="center" align="center"></el-table-column>
      <el-table-column prop="grade" width="100" label="奖等" header-align="center" align="center"></el-table-column>
      <el-table-column prop="certificate" width="100" label="证书编号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="status" width="120" label="状态" header-align="center" align="center">
        <template v-slot="scope">
          <el-tag v-if="scope.row.status === 0" size="small" type="">审核中</el-tag>
          <el-tag v-else-if="scope.row.status === 1" size="small" type="">报名成功</el-tag>
          <el-tag v-else-if="scope.row.status === 2" size="small" type="">完赛</el-tag>
          <el-tag v-else-if="scope.row.status === 3" size="small" type="">已取消/弃权</el-tag>
          <el-tag v-else-if="scope.row.status === 4" size="small" type="">比赛中</el-tag>
          <el-tag v-else-if="scope.row.status === 5" size="small" type="">已获晋级赛资格</el-tag>
          <el-tag v-else-if="scope.row.status === 6" size="small" type="">晋级成功</el-tag>
          <el-tag v-else-if="scope.row.status === 7" size="small" type="">弃权</el-tag>
          <el-tag v-else-if="scope.row.status === 8" size="small" type="">驳回</el-tag>
          <el-tag v-else-if="scope.row.status === 9" size="small" type="">待确认</el-tag>
          <el-tag v-else-if="scope.row.status === 10" size="small" type="">报名已截止</el-tag>
          <el-tag v-else size="small" type="">其他</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle"> </el-pagination>
  </div>
</template>

<script lang="ts">
import useView from "@/hooks/useView";
import { defineComponent, reactive, toRefs } from "vue";
import baseService from "@/service/baseService";
export default defineComponent({
  setup() {
    const state = reactive({
      getDataListURL: "/sp/spmatchitemteam/signRecordStudent",
      createdIsNeed: false,
      activatedIsNeed: false,
      getDataListIsPage: true,
      exportURL: "/sp/spmatchitemteam/export",
      deleteURL: "/sp/spmatchitemteam",
      deleteIsBatch: true,
      dataForm: {
        matchId: "",
        phone: "",
        phone2: "",
        matchItemId: ""
      },
      activeName: "first",
      detailVisible: false,
      detailId: "",
      baseMatchList: [],
      teamVisible: false,
      userVisible: false,
      scores: [],
      matchItem: {
        match_name: "",
        item_name: ""
      }
    });
    return { ...useView(state), ...toRefs(state) };
  },
  created() {
    this.queryMatchItemList();
    this.queryBymatchItemId(this.dataForm.matchItemId);
  },
  methods: {
    // 赛项
    queryMatchItemList() {
      baseService.get("/sp/spmatchitem/page", { page: 1, limit: 100, matchId: this.dataForm.matchId }).then((e: any) => {
        this.baseMatchList = e.data.list;
      });
    },
    submit() {
      baseService.get(`/sp/spticket/rebuild`, this.dataForm).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.$message.success("操作成功");
      });
    },
    /**
     * 查询score配置
     * @param matchItemId
     */
    queryBymatchItemId(matchItemId: any) {
      baseService.post("/sp/spmatchitem/queryBymatchItemId", { id: matchItemId }).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        } else {
          console.log(res.data);
          this.matchItem = res.data;
          // this.dataForm.fileName = this.matchItem.item_name +'('+ this.dataForm.groupNameFile + ')';
          this.scores = res.data.scores.split(",");
        }
      });
    },
    generateCertificate() {
      baseService.get(`/sp/spmatchitemteam/certificateOne`, this.dataForm).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.$message.success("操作成功");
      });
    },

    teamHandle(id?: string) {
      this.teamVisible = true;
      baseService.get("/sp/spteam/" + id).then((e: any) => {
        this.$nextTick(() => {
          if (e.data) {
            e.data.sex = e.data.sex == 0 ? "女" : "男";
          }
          this.$refs.teamRef.dataForm.row = e.data;
          this.$refs.teamRef.init();
        });
      });
    },

    userHandle(id?: string) {
      this.userVisible = true;
      baseService.get("/sp/spuser/" + id).then((e: any) => {
        this.$nextTick(() => {
          if (e.data) {
            e.data.sex = e.data.sex == 0 ? "女" : "男";
          }
          this.$refs.userRef.dataForm.row = e.data;
          this.$refs.userRef.init();
        });
      });
    }
  }
});
</script>
